package com.solute.web.sso.regist;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.solute.domain.RegistVO;
import com.solute.domain.UserVO;
import com.solute.domain.ValidVO;
import com.solute.utils.regex.RegexConstants;
import com.solute.web.sessions.User;
import com.solute.web.sso.regist.service.RegistService;

@Controller
@RequestMapping(value="/regist")
public class RegistController {
	
	private static final Logger logger = LoggerFactory.getLogger(RegistController.class);
	
	private @Autowired RegistService registService;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView main() {
		logger.info("regist page opened");
		
		ModelAndView mav = new ModelAndView("regist", "command", new RegistVO());
		
		return mav;
	}
	
	@RequestMapping(value="/validation", method=RequestMethod.POST)
	public @ResponseBody
	List<?> valid(HttpServletRequest request, @ModelAttribute RegistVO registVO, BindingResult result) {
		logger.info("valid called");
		
		List<ValidVO> valids = new LinkedList<ValidVO>();
	
		if(!StringUtils.hasText(registVO.getEmail()))
			valids.add(new ValidVO("email", "이메일을 써주십시오."));
		else if(!registVO.getEmail().matches(RegexConstants.EMAIL_FORMAT))
			valids.add(new ValidVO("email", "이메일의 형식을 맞춰 써주십시오."));
		else if(!StringUtils.hasText(registVO.getReEmail()))
			valids.add(new ValidVO("reEmail", "이메일을 다시 한번 써주십시오."));
		else if(!registVO.getEmail().matches(RegexConstants.EMAIL_FORMAT))
			valids.add(new ValidVO("reEmail", "이메일의 형식을 맞춰 써주십시오."));
		else if(!registVO.getEmail().equals(registVO.getReEmail()))
			valids.add(new ValidVO("reEmail", "이메일이 맞지 않습니다."));
		else if(registService.existsEmail(registVO.getEmail()))
			valids.add(new ValidVO("email", "이메일이 존재합니다."));
		else if(!StringUtils.hasText(registVO.getPassword()))
			valids.add(new ValidVO("password", "패스워드를 입력해주십시오."));
		else if(!StringUtils.hasText(registVO.getRePassword()))
			valids.add(new ValidVO("rePassword", "패스워드를 다시 한번 입력해주십시오."));
		else if(!registVO.getPassword().equals(registVO.getRePassword()))
			valids.add(new ValidVO("rePassword", "패스워드가 맞지 않습니다."));
		
		logger.debug("email : {}", registVO.getEmail());
		
		return valids;
	}
	
	@RequestMapping(value="/submit", method=RequestMethod.POST)
	public @ResponseBody
	void regist(@ModelAttribute UserVO userVO, BindingResult result, ModelMap modelMap) {
		if(result.hasErrors()) {
			logger.error("regist error!! (userVO : {})", userVO);
			throw new RuntimeException();
		}
		
		if(!registService.regist(userVO))
			throw new RuntimeException();
		
	}
}
